<template>
  <div>
    <BasicTable
      :pagination="false"
      @register="registerTable"
      v-model:expandedRowKeys="expandedRowKeys"
    >
      <template #toolbar>
        <a-button v-if="hasPermission('dept:save')" type="primary" @click="handleCreate">
          新增部门
        </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                label: '编辑',
                auth: 'dept:update',
                icon: 'clarity:note-edit-line',
                onClick: handleEdit.bind(null, record),
              },
              {
                label: '新增',
                auth: 'dept:save',
                icon: 'clarity:add-line',
                onClick: handleCreate.bind(null, record),
              },
              {
                label: '删除',
                auth: 'dept:delete',
                icon: 'ant-design:delete-outlined',
                color: 'error',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
                ifShow: (_action) => {
                  return record.parentId !== '0';
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <SysDeptModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup>
  // import { nextTick, onMounted } from 'vue';
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { useModal } from '@/components/Modal';
  import SysDeptModal from './sys-dept-modal.vue';
  import { listDeptApi, delDeptApi } from '@/api/system/dept';
  import { columns, searchFormSchema } from './data';
  import { message } from 'ant-design-vue';
  import { usePermission } from '@/hooks/web/usePermission';
  import { ref } from 'vue';

  const { hasPermission } = usePermission();
  const expandedRowKeys = ref<string[]>([]);
  const [registerTable, { reload, getSelectRows, expandAll }] = useTable({
    title: '部门管理',
    api: listDeptApi,
    afterFetch: () => {
      setTimeout(() => {
        expandAll();
      }, 0);
    },
    isTreeTable: true,
    columns,
    formConfig: {
      labelWidth: 70,
      schemas: searchFormSchema,
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    showIndexColumn: false,
    actionColumn: {
      width: 240,
      title: '操作',
      dataIndex: 'action',
      fixed: undefined,
    },
  });

  const [registerModal, { openModal }] = useModal();

  function handleCreate(record) {
    if (record.ancestors) {
      openModal(true, { ancestors: record.ancestors + ',' + record.id });
    } else {
      openModal(true);
    }
  }

  function handleEdit(record: Recordable) {
    openModal(true, record);
  }

  async function handleDelete(record: Recordable) {
    if (record.id) {
      await delDeptApi(record.id);
    } else {
      const delData = getSelectRows();
      if (delData.length === 0) {
        message.warning('请选择数据');
        return;
      }
      const params: any = [];
      delData.map((item) => {
        params.push(item.id);
      });
      await delDeptApi(params.join(','));
    }
    reload();
  }

  function handleSuccess() {
    reload();
  }
</script>
